package com.tim.app.ui.activity.some_homepage;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.HorizontalDividerItemDecoration;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.ShareInfo;
import com.tim.app.server.entry.User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.adapter.someone_homepage.HomepageShareAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * 某人的故事页面
 */
public class SomeoneShareActivity extends BaseActivity implements LoadMoreHandler,BaseRecyclerAdapter.OnItemClickListener {

    public static final String TAG = "SomeoneStoryActivity";

    private ImageButton ibBack;
    private TextView tvTitle;
    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvStory;

    private HomepageShareAdapter adapter;
    private List<ShareInfo> dataList;
    private EmptyLayout emptyLayout;

    private User currentUser;

    public static void start(Context context, User user) {
        Intent intent = new Intent(context, SomeoneShareActivity.class);
        intent.putExtra(AppKey.USER, user);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_someone_story;
    }


    @Override
    public void initView() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        ibBack = (ImageButton) findViewById(R.id.ibBack);
        tvTitle = (TextView) findViewById(R.id.tvTitle);

        load_more = (LoadMoreRecycleViewContainer) findViewById(R.id.load_more);
        wrvStory = (WrapRecyclerView) findViewById(R.id.wrvStory);
        wrvStory.setOverScrollMode(View.OVER_SCROLL_NEVER);

        load_more.useDefaultFooter(View.GONE);
        load_more.setAutoLoadMore(true);
        load_more.setLoadMoreHandler(this);

        emptyLayout = new EmptyLayout(this, load_more);
//        emptyLayout.showLoading();
        emptyLayout.setEmptyButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.showLoading();
//                    API_Home.ins().getHomepageData(TAG, 0, PAGE_SIZE, CacheMode.FIRST_CACHE_THEN_REQUEST, refreshCallback);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvStory.setLayoutManager(layoutManager);

        wrvStory.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(getResources().getColor(R.color.view_background_color)).size((int) getResources().getDimension(R.dimen.dimen_5)).build());

        dataList = new ArrayList<>();
        adapter = new HomepageShareAdapter(this, dataList);
        adapter.setOnItemClickListener(this);
        wrvStory.setAdapter(adapter);

        ibBack.setOnClickListener(this);
        currentUser = (User) getIntent().getSerializableExtra(AppKey.USER);

        if (null != currentUser) {
            if (UserManager.ins().getUserId().equals(currentUser.getUserId())) {
                tvTitle.setText(getString(R.string.myQuestion));
            } else {
                if (!TextUtils.isEmpty(currentUser.getName())) {
                    tvTitle.setText(getString(R.string.someoneQuestion, currentUser.getName()));
                }
            }
        }
        createTestData();
    }

    private void createTestData() {
        for (int i = 0; i < 10; i++) {
            ShareInfo data = new ShareInfo();
            data.setShareDesc("我觉得2016-2017赛季的NBA总冠军还是骑士的，杜兰特注定终生无冠!");
            data.setTitle("中国全力以赴能否在局部地区打赢美国？");
            data.setContent("一寸山河一寸血，十万青年十万军。宁可华夏遍地坟，也叫鬼子重做人！");
            data.setPriseNumber(new Random().nextInt(100));
            data.setCommentNumber(new Random().nextInt(200));
            data.setCtime(System.currentTimeMillis());
            data.setAuthor(new User());
            data.setAudioTime(1000L);
            data.setCoverImageUrl("http://pic28.nipic.com/20130424/11588775_115415688157_2.jpg");
            data.setAudioUrl("http://www.baidu.com");
            if (i == 0) {
                data.setType(ShareInfo.TYPE_STORY);
            } else if (i == 1) {
                data.setType(ShareInfo.TYPE_ARTICLE);
            }else if (i == 2) {
                data.setCoverImageUrl("");
                data.setType(ShareInfo.TYPE_ARTICLE);
            }else if (i == 3) {
                data.setType(ShareInfo.TYPE_SUNJCET);
            }else if (i == 4) {
                data.setCoverImageUrl("");
                data.setType(ShareInfo.TYPE_SUNJCET);
            }else if (i == 5) {
                data.setCoverImageUrl("");
                data.setType(ShareInfo.TYPE_QUESTION);
            }else{
                data.setType(ShareInfo.TYPE_STORY);
            }
            dataList.add(data);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View view, int position, long id) {

    }

    @Override
    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibBack://返回
                finish();
                break;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

}
